class Solution(object):
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        lookup = {
            'I': 1,
            'V': 5,
            'X': 10,
            'L': 50,
            'C': 100,
            'D': 500,
            'M': 1000
        }

        res = 0

        # for i in range(len(s)):
        #     if i > 0 and lookup[s[i]] > lookup[s[i - 1]]:
        #         res += lookup[s[i]] - 2 * lookup[s[i - 1]]
        #     else:
        #         res += lookup[s[i]]
        # return res
        for i in range(len(s)-1):
            if lookup[s[i]] >= lookup[s[i + 1]]:
                res+=lookup[s[i]]
            else:
                res += lookup[s[i+1]] -  lookup[s[i]]
                i+=2
        return res

a = "III"
s=Solution()
m=s.romanToInt(a)
print(m)